<template>
  <my-drawer ref="myRef" title="轮播图分类" @submit="submit" :disabled="false" width="30%">
    <el-form :model="drawerProps.row" label-width="120" ref="formRef">
      <el-form-item label="轮播图分类名称">
        <el-input v-model="drawerProps.row.name"></el-input>
      </el-form-item>
      <el-form-item label="排序值">
        <el-input-number v-model="drawerProps.row.sort" :min="1" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="是否出现">
        <el-switch
          v-model="drawerProps.row.isShow"
          :active-value="true"
          :inactive-value="false"
          active-text="是"
          inactive-text="否"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否有封面">
        <el-switch
          v-model="drawerProps.row.isCover"
          :active-value="true"
          :inactive-value="false"
          active-text="是"
          inactive-text="否"
        ></el-switch>
      </el-form-item>
      <el-form-item label="类型所属">
        <el-select v-model="drawerProps.row.type">
          <el-option v-for="(p, index) in typeList" :key="index" :label="p.label" :value="p.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </my-drawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive } from "vue";

const typeList = reactive([
  { label: "楼盘", value: 1 },
  { label: "户型", value: 2 }
]);

interface DrawerProps {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const initData: any = {
  name: "",
  type: 0,
  sort: 1,
  isShow: true
};

const drawerProps = ref<DrawerProps>({ row: JSON.parse(JSON.stringify(initData)) });

const myRef = ref<InstanceType<typeof MyDrawer> | null>();

const acceptParams = (params: any) => {
  drawerProps.value = params;
  myRef.value?.acceptParams();
};

const formRef = ref<FormInstance>();

const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (valid) {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success("成功");
      await drawerProps.value.getTable!();
      myRef.value?.close();
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style></style>
